package com.google.shoopinggoods.fragment;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.google.shoopinggoods.GoodsDetailActivity;
import com.google.shoopinggoods.adpter.GoodslistAdapter;
import com.google.shoopinggoods.bean.Goods;
import com.google.shoopinggoods.global.Url;
import com.google.shoopinggoods.utils.KLog;
import com.lxj.okhttpengine.OkHttpEngine;
import com.lxj.okhttpengine.PostParams;
import com.lxj.okhttpengine.callback.OkHttpCallback;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;

/**
 * Created by Administrator on 2017/10/14.
 */
public class GoodsListFragment extends BaseListFragment implements GoodslistAdapter.OnGoodsItemClickListener {

    public static String CATEGORY="category";
    private String category;
    private ArrayList list = new ArrayList();
    int page = 0;

    @Override
    public void init() {
        super.init();
        category = getArguments().getString(CATEGORY);
        ((GoodslistAdapter)mAdapter).setOnGoodsItemClickListener(this);
    }

    @Override
    public String getUrl() {
        String url = String.format(Url.getGoodsList,category,isRefresh?0:page);
        KLog.e(KLog.Tag.QUICKEY,"GoodListFragment url:"+url);
        return url;
    }

    @Override
    public PostParams getPostParams() {
        return null;
    }

    @Override
    public RecyclerView.Adapter getAdapter() {
        return new GoodslistAdapter(list);
    }

    @Override
    public OkHttpCallback getCallback() {
        return callback;
    }

    private OkHttpCallback<GoodsList> callback = new OkHttpCallback<GoodsList>() {
        @Override
        public void onSuccess(GoodsList goodsList) {
            stateLayout.showContentView();
if (goodsList==null){return;}
            if (goodsList.isSuccess()){
                if (goodsList.data!=null&&goodsList.data.size()>0){
                    //如果是下拉刷新，则清空原有集合
                    if (isRefresh){
                        list.clear();
                        isRefresh=false;
                    }
                    list.addAll(goodsList.data);
                    page++;
                    mAdapter.notifyDataSetChanged();
                    recyclerView.refreshComplete();
                    recyclerView.loadMoreComplete();
                }else{
                    recyclerView.refreshComplete();
                    recyclerView.setNoMore(true);
                }
            }else {
                recyclerView.refreshComplete();
                recyclerView.loadMoreComplete();
            }
        }

        @Override
        public void onFail(IOException e) {

            if (list.size() == 0) {
                stateLayout.showErrorView();
            } else {
                Toast.makeText(getContext(),"加载失败，请重试",Toast.LENGTH_SHORT).show();
            }
            recyclerView.refreshComplete();
            recyclerView.loadMoreComplete();
        }
    };

    @Override
    public void onItemClick(int position) {
        Intent intent = new Intent(getContext(), GoodsDetailActivity.class);
        intent.putExtra("goods", (Serializable) list.get(position));
        startActivity(intent);
    }
}
